﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Profile;
using System.IO;

namespace Continental.Admin
{
    public partial class ManagerUserDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                String userName = Request.QueryString["UserName"].ToString();
                ltlUserName.Text = userName;

                MembershipUser theUser = Membership.GetUser(userName);
                ProfileBase theProfile = ProfileBase.Create(userName);
                ltlFullName.Text = theProfile.GetPropertyValue("FullName").ToString();
                ltlMobile.Text = theProfile.GetPropertyValue("Mobile").ToString();
                String avartarURL = theProfile.GetPropertyValue("Avartar").ToString();
                avartarURL = Path.Combine("~/ImageData/Avartar/", avartarURL);
                if (File.Exists(Server.MapPath(avartarURL)))
                {
                    imgAvartar.ImageUrl = avartarURL;
                }
                ltlGender.Text = theProfile.GetPropertyValue("Gender").ToString();
                ltlDateOfBirth.Text = theProfile.GetPropertyValue("Birthday").ToString();
                //ltlCity.Text = theUser.GetPropertyValue().ToString();
                ltlYahoo.Text = theProfile.GetPropertyValue("Yahoo").ToString();
                ltlSkype.Text = theProfile.GetPropertyValue("Skype").ToString();
                ltlStatus.Text = theUser.IsApproved.ToString();

                ddlRole.DataSource = Roles.GetAllRoles();
                String[] selectedRoleList = Roles.GetRolesForUser(userName);
                String selectedRole = selectedRoleList[0];
                ddlRole.SelectedValue = selectedRole;
                ltlRole.Text = selectedRole;
                ddlRole.DataBind();

                if (!Roles.IsUserInRole(userName, "Contributor"))
                {
                    ltlPopup.Visible = false;
                }
                else
                {
                    ltlPopup.Visible = true;
                }

                // thiet dinh trang thai
                if (!theUser.IsApproved)
                {
                    lblMessage.Text = "Tài khoản " + userName + " đã bị khóa";
                    btnDisable.Text = "Kích hoạt người dùng";
                }
            }
            Page.DataBind();
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            String userName = Request.QueryString["UserName"].ToString();
            String[] selectedRoleList = Roles.GetRolesForUser(userName);
            String selectedRole = selectedRoleList[0];
            if (selectedRole != ddlRole.SelectedValue)
            {
                Roles.AddUserToRole(userName, ddlRole.SelectedValue);
                Roles.RemoveUserFromRole(userName, selectedRole);
                ltlRole.Text = ddlRole.SelectedValue;
            }
            if (!String.Equals(ddlRole.SelectedValue, "Contributor"))
            {
                ltlPopup.Visible = false;
            }
            else
            {
                ltlPopup.Visible = true;
            }
        }

        protected void btnDisable_Click(object sender, EventArgs e)
        {
            String userName = Request.QueryString["UserName"].ToString();
            MembershipUser theUser = Membership.GetUser(userName);
            if (theUser.IsApproved)
            {
                theUser.IsApproved = false;
                lblMessage.Text = "Tài khoản " + userName + " đã bị khóa";
                btnDisable.Text = "Kích hoạt người dùng";
            }
            else
            {
                theUser.IsApproved = true;
                lblMessage.Text = "Tài khoản " + userName + " đã được kích hoạt trở lại";
                btnDisable.Text = "Khóa người dùng";
            }
            Membership.UpdateUser(theUser);
            ltlStatus.Text = theUser.IsApproved.ToString();
        }

        protected void ddlRole_PreRender(object sender, EventArgs e)
        {
            String userName = Request.QueryString["UserName"].ToString();
            String[] selectedRoleList = Roles.GetRolesForUser(userName);
            String selectedRole = selectedRoleList[0];
            ListItem selectedItem = ddlRole.Items.FindByValue(selectedRole);
            ddlRole.SelectedIndex = ddlRole.Items.IndexOf(selectedItem);
        }
    }
}
